set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_VERSION 1)
cmake_minimum_required(VERSION 3.26)

# specify cross-compilers and tools
set(CMAKE_C_COMPILER arm-none-eabi-gcc)
set(CMAKE_CXX_COMPILER arm-none-eabi-g++)
set(CMAKE_ASM_COMPILER arm-none-eabi-gcc)
set(CMAKE_AR arm-none-eabi-ar)
set(CMAKE_OBJCOPY arm-none-eabi-objcopy)
set(CMAKE_OBJDUMP arm-none-eabi-objdump)
set(SIZE arm-none-eabi-size)
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)

#set(CMAKE_CXX_COMPILER_LAUNCHER ccache)
#set(CMAKE_C_COMPILER_LAUNCHER ccache)

# project settings
project(at32f413_template C CXX ASM)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_C_STANDARD 11)

set(CPU -mcpu=cortex-m4)
set(FPU -mfpu=fpv4-sp-d16)
set(FLOAT-ABI -mfloat-abi=hard)

set(MCU ${CPU} -mthumb ${FPU} ${FLOAT-ABI})
set(LINKER_SCRIPT ${CMAKE_SOURCE_DIR}/project/misc/AT32F413xB_FLASH.ld)

add_definitions(-DAT_START_F413_V1
        -DAT32F413CBT7
        -DUSE_STDPERIPH_DRIVER)

include_directories("project/inc"
        "project/board"
        "libraries/cmsis/cm4/core_support"
        "libraries/cmsis/cm4/device_support"
        "libraries/drivers/inc")

file(GLOB_RECURSE SOURCES
        "project/src/*.c"
        "project/board/*.c"
        "libraries/cmsis/cm4/device_support/system_at32f413.c"
        "libraries/drivers/src/*.c"
        "project/src/startup_at32f413.s")

add_compile_options(${MCU})
add_compile_options(-Wall -fdata-sections -ffunction-sections)
# Wno
add_compile_options(-Wno-missing-braces)
add_compile_options(-Wno-format)

if ("${CMAKE_BUILD_TYPE}" STREQUAL "Release")
    message(STATUS "Maximum optimization for speed")
    # flto
    #    add_compile_options(-flto)
    add_compile_options(-O3)
elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "RelWithDebInfo")
    message(STATUS "Maximum optimization for speed, debug info included")
    add_compile_options(-Ofast -g)
elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "MinSizeRel")
    message(STATUS "Maximum optimization for size")
    add_compile_options(-Os)
else ()
    message(STATUS "Minimal optimization, debug info included")
    add_compile_options(-O0 -g -gdwarf-2)
endif ()

add_link_options(${MCU})
#add_link_options(--specs=nosys.specs)
add_link_options(--specs=nano.specs -u_printf_float)
add_link_options(-T ${LINKER_SCRIPT})
add_link_options(-lc -lm)
add_link_options(-Wl,--print-memory-usage,-Map=${PROJECT_BINARY_DIR}/${PROJECT_NAME}.map,--cref)
add_link_options(-Wl,--gc-sections)

add_executable(${PROJECT_NAME}.elf ${SOURCES} ${LINKER_SCRIPT})

set(HEX_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.hex)
set(BIN_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.bin)

add_custom_command(TARGET ${PROJECT_NAME}.elf POST_BUILD
        COMMAND ${CMAKE_OBJCOPY} -Oihex $<TARGET_FILE:${PROJECT_NAME}.elf> ${HEX_FILE}
        COMMAND ${CMAKE_OBJCOPY} -Obinary $<TARGET_FILE:${PROJECT_NAME}.elf> ${BIN_FILE}
        COMMAND ${SIZE} --format=berkeley $<TARGET_FILE:${PROJECT_NAME}.elf>
        COMMENT "Building ${HEX_FILE}
Building ${BIN_FILE}")

add_custom_command(OUTPUT upload
        COMMENT "Upload $<TARGET_FILE:${PROJECT_NAME}.elf>"
        COMMAND pyocd load $<TARGET_FILE:${PROJECT_NAME}.elf> --config ../pyocd.yaml
        USES_TERMINAL
)

add_custom_target(swd_upload
        DEPENDS ${PROJECT_NAME}.elf upload
)

add_custom_target(swd_erase
        COMMENT "Erase Chip"
        COMMAND pyocd erase --chip --config ../pyocd.yaml
        USES_TERMINAL
        VERBATIM
)
